﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Xml;
using System.IO;

namespace HasuSLLib
{
    public class DynamicWCFCall
    {
        //http://developergeeks.com/article/38/calling-wcf-service-using-client-channnel-factory
        public  static IClientChannel GetClientChannel<T>(string serviceUri)
        {
            return (IClientChannel)CreateChannelFactory<T>(serviceUri).CreateChannel();
        }

        public static ChannelFactory<T> CreateChannelFactory<T>(string url)
        {
            HttpTransportBindingElement httpTransport = new HttpTransportBindingElement 
            {  
               // MaxBufferPoolSize = int.MaxValue,
                MaxBufferSize = int.MaxValue,
                MaxReceivedMessageSize = int.MaxValue
            };

            TextMessageEncodingBindingElement messageEncoding = new TextMessageEncodingBindingElement
            {
                //MaxReadPoolSize = int.MaxValue,
                //MaxWritePoolSize = int.MaxValue,
                MessageVersion = MessageVersion.Soap11,
                WriteEncoding = System.Text.Encoding.UTF8
            };

            //-- Use this binding incase your service required CustomBinding configuration.
            CustomBinding binding = new CustomBinding(messageEncoding, httpTransport);

            //-- Else use WSHttpBinding configuration.
           // return new ChannelFactory<T>(new WSHttpBinding(), new EndpointAddress(url));
            return new ChannelFactory<T>(binding, new EndpointAddress(url));
        }
    }
}
